﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenGado.Business;
using OpenGado.DATA.Entities;
namespace OpenGado
{
    public partial class UnitofMeasureForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            // check the state of hte page open list / edit 

            if (!IsPostBack)
            {
                switch (Request.QueryString["action"])
                {
                    case "edit":
                        GetUnitItem();
                        pnlForm.Visible = true;
                        pnlList.Visible = false;
                     
                        // show item to edit
                        break;

                    case "add":
                        pnlForm.Visible = true;
                        pnlList.Visible = false;
                        break;

                    case "delete":
                        DeleteItem();
                         pnlList.Visible = true;
                        pnlForm.Visible = false;
                        break;
                    default:
                        pnlList.Visible = true;
                        pnlForm.Visible = false;
                        break;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveUnitItem();
        }

        private void GetUnitItem()
        {
            txtUnitId.Value = Request.QueryString["id"];
            
            UnitOfMeasureBusiness ub = new UnitOfMeasureBusiness();
            UnitOfMeasure um = new UnitOfMeasure();
             
            um = ub.GetUnitById(int.Parse(Request.QueryString["id"].ToString()));

            txtUnitId.Value = um.Id.ToString();
            txtUnit.Text = um.Name;
           

        }

        private void DeleteItem()
        {
            int delId;
            int.TryParse(Request.QueryString["id"].ToString(), out delId);

            UnitOfMeasureBusiness ub = new UnitOfMeasureBusiness();
            UnitOfMeasure um = new UnitOfMeasure();
            um.Id = delId;
            ub.Delete(um);
        }

        private  void SaveUnitItem()
        {
            UnitOfMeasure um = new UnitOfMeasure();

            if (!String.IsNullOrEmpty(txtUnitId.Value))
            {
                um.Id = int.Parse(txtUnitId.Value);
            }
          
            um.Name = txtUnit.Text;
            UnitOfMeasureBusiness ub = new UnitOfMeasureBusiness();
            try
            {
               ub.Save(um);
            }
            catch (Exception)
            {
                

                
            }
        }

       
    }
}